package com.yunxi.controller.app;


import com.github.pagehelper.PageInfo;
import com.yunxi.app.pojo.VersionVO;
import com.yunxi.app.service.VersionDubboService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by leed on 2018/10/23.
 */
@RestController
@RequestMapping("/admin/app/version")
@Api(tags = "app:version", description = "版本管理")
public class VersionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(VersionController.class);

    @Autowired
    private VersionDubboService versionDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询app版本列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query")

    })
    public ApiResult<PageInfo<VersionVO>> page(VersionVO versionVO, Integer pageNum, Integer pageSize) throws Exception {
        LOGGER.debug("分页查询app版本列表");
        return ResponseUtil.ok(versionDubboService.findPageByVO(versionVO, pageNum, pageSize));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询app版本列表")
    public ApiResult<List<VersionVO>> list(VersionVO versionVO) throws Exception {
        return ResponseUtil.ok(versionDubboService.listByVo(versionVO));
    }

    @GetMapping("/load")
    @ApiOperation(value = "查询app版本列表")
    public ApiResult load(@RequestParam Long id) throws Exception {
        return ResponseUtil.ok(versionDubboService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation("创建或编辑app版本")
    public ApiResult save(@RequestBody VersionVO versionVO) {
        LOGGER.debug("创建或编辑app版本");
        ApiResult object;
        try {
            String pkId = versionVO.getPkId();
            if (pkId != null && !pkId.equals("")) {
//                versionVO.setCreateMan(user.getUserName());
                object = ResponseUtil.ok("创建成功", null);
            } else {
                object = ResponseUtil.ok("编辑成功", null);
            }
            versionDubboService.saveOrUpdate(versionVO);
        } catch (Exception e) {
            object = ResponseUtil.fail502();
            LOGGER.error("创建或编辑版本异常", e.fillInStackTrace());
        }

        return object;
    }

    @PostMapping("/status")
    @ApiOperation("更新版本状态（禁用,启用）")
    public ApiResult status(@RequestParam String pkId) {
        LOGGER.debug("更新版本状态");
        ApiResult o;
        try {
            VersionVO version = versionDubboService.getById(Long.valueOf(pkId));
//            if (BaseEntity.AvailableStatus.available == version.getStatus())
//                version.setStatus(BaseEntity.AvailableStatus.unavailable);
//            else
//                version.setStatus(BaseEntity.AvailableStatus.available);

            Integer status = version.getStatus();
            if (status == 0) {
                //禁用，变为启用
                version.setStatus(1);
            } else {
                version.setStatus(0);
            }
//            versionService.updateByPrimaryKeySelective(version);
            versionDubboService.saveOrUpdate(version);
            o = ResponseUtil.ok("更新成功", null);
        } catch (Exception e) {
            o = ResponseUtil.fail502();
            LOGGER.error("更新版本状态异常", e.fillInStackTrace());
        }
        return o;
    }

    @GetMapping("/getNewVersion")
    @ApiOperation(value = "获取最新版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "app版本类型(0PDA 1销售助手)", required = true, paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台类型（android,ios）", required = true, paramType = "query")

    })
    public ApiResult getNewVersion(Integer type, String platform) throws Exception {
        return ResponseUtil.ok(versionDubboService.getNewVersion(type, platform));
    }

    /**
     * 删除
     *
     * @param versionList 批量版本
     * @return
     */
    @PostMapping("/del")
    @ApiOperation("删除版本信息")
    public ApiResult delete(@RequestBody List<VersionVO> versionList) {
        LOGGER.debug("删除版本信息");
        ApiResult o;
        try {
            /*if (versionList != null) {
                for (Version v : versionList) {
                    versionService.removeById(v.getPkId());
                }
            }*/
            versionDubboService.removeByListVo(versionList);
            o = ResponseUtil.ok("删除成功", null);
        } catch (Exception e) {
            o = ResponseUtil.fail502();
            LOGGER.error("删除版本状态异常", e.fillInStackTrace());
        }
        return o;
    }
}
